package com.foosun.core.base;

import org.apache.commons.lang.StringUtils;

import java.util.*;

/**
 * Created by Nelin on 14-2-27.
 */

import java.util.List;

/**
 * 页面数据
 */
public class PageData<T> {
	private List<T> result;// 页面数据列表
	private Pagination pagination = new Pagination();// 分页

	private Map<String, List<Object>> sqlMap;
	private Map<String, Map> sqlTotalMap;

	private Map<String, Object> webMap;

	private String orderName;
	private String ascDesc;

	/**
	 * 获取页面数据列表.
	 */
	public List<T> getResult() {
		return result;
	}

	/**
	 * 设置页面数据列表.
	 */
	public void setResult(List<T> result) {
		this.result = result;
	}

	/**
	 * 获取分页信息.
	 */
	public Pagination getPagination() {
		return pagination;
	}

	/**
	 * 设置分页信息.
	 */
	public void setPagination(Pagination pagination) {
		this.pagination = pagination;
	}

	/**
	 * 设置当前页号,序号从1开始,低于1时自动调整为1.
	 */
	public void setPageNo(int pageNo) {
		pagination.setPageNo(pageNo);
	}

	/**
	 * 设置每页的记录数量,低于2时自动调整为20.
	 */
	public void setPageSize(int pageSize) {
		pagination.setPageSize(pageSize);
	}

	public void setWebMap(Map<String, Object> webMap) {
		this.webMap = webMap;
		sqlMap = new HashMap<String, List<Object>>();
		sqlTotalMap = new HashMap<String, Map>();
		Iterator<String> iterable = webMap.keySet().iterator();
		while (iterable.hasNext()) {
			String key = iterable.next();
			Object value = webMap.get(key);
			if (value == null)
				continue;
			// 注意sql注入
			if (key.startsWith("LIKE_S&")) {
				String v = "";
				if (value instanceof String[]) {
					v = ((String[]) value)[0];
				} else {
					continue;
				}
				if (StringUtils.isBlank(v)) {
					continue;
				}
				v = v.trim();
				key = key.substring(7);
				String[] vars = key.split("_OR_");
				String sql = "";
				String total = "";
				List<Object> params = new ArrayList();
				v = "%" + v + "%";
				Map totalParams = new HashMap();
				for (String p : vars) {
					sql += "`" + p + "` LIKE ? OR ";
					params.add(v);
					total += "`" + p + "` LIKE :" + p + " OR ";
					totalParams.put(p, v);
				}
				sql = sql.substring(0, sql.length() - 3);
				total = total.substring(0, total.length() - 3);
				sqlMap.put("(" + sql + ")", params);
				sqlTotalMap.put("(" + total + ")", totalParams);
			} else if (key.startsWith("EQ_I&")) {
				Integer v = 0;
				if (value instanceof String[]) {
					v = Integer.parseInt(((String[]) value)[0]);
					if (v == 0) {
						continue;
					}
				} else if (value instanceof Integer) {
					v = (Integer) value;
				} else {
					continue;
				}
				key = key.substring(5);
				String sql = "";
				String total = "";
				List<Object> params = new ArrayList();
				Map totalParams = new HashMap();
				sql += "`" + key + "` = ? ";
				params.add(v);
				total += "`" + key + "` = :" + key + " ";
				totalParams.put(key, v);
				sqlMap.put("(" + sql + ")", params);
				sqlTotalMap.put("(" + total + ")", totalParams);
			} else if (key.startsWith("EQS_I&")) {
				List<Integer> v = null;
				if (value instanceof List) {
					v = (List<Integer>) value;
				} else if (value instanceof Integer[]) {
					v = Arrays.asList((Integer[]) value);
				} else {
					continue;
				}
				if (v == null || v.isEmpty()) {
					v = new ArrayList<Integer>();
					v.add(0);
				}
				key = key.substring(6);
				String sql = "";
				String total = "";
				List<Object> params = new ArrayList();
				Map totalParams = new HashMap();
				int i = 0;
				for (Integer vv : v) {
					sql += "or `" + key + "` = ? ";
					params.add(vv);
					String p = key + i;
					total += "or `" + key + "` = :" + p + " ";
					totalParams.put(p, vv);
					i++;
				}
				sql = sql.substring(3);
				total = total.substring(3);

				sqlMap.put("(" + sql + ")", params);
				sqlTotalMap.put("(" + total + ")", totalParams);
			} else if (key.startsWith("EQS_NO_I&")) {
				List<Integer> v = null;
				if (value instanceof List) {
					v = (List<Integer>) value;
					if (v == null) {
						continue;
					}
				} else {
					continue;
				}
				key = key.substring(9);
				String sql = "";
				String total = "";
				List<Object> params = new ArrayList();
				Map totalParams = new HashMap();
				int i = 0;
				for (Integer vv : v) {
					sql += "and `" + key + "` != ? ";
					params.add(vv);
					String p = key + i;
					total += "and `" + key + "` != :" + p + " ";
					totalParams.put(p, vv);
					i++;
				}
				sql = sql.substring(4);
				total = total.substring(4);

				sqlMap.put("(" + sql + ")", params);
				sqlTotalMap.put("(" + total + ")", totalParams);
			} else if (key.startsWith("EQ_S&")) {
				String v = "";
				if (value instanceof String[]) {
					v = ((String[]) value)[0];
					if (StringUtils.isBlank(v)) {
						continue;
					}
				} else if (value instanceof String) {
					v = (String) value;
					if (StringUtils.isBlank(v)) {
						continue;
					}
				} else {
					continue;
				}
				v = v.trim();
				key = key.substring(5);
				String sql = "";
				String total = "";
				List<Object> params = new ArrayList();
				Map totalParams = new HashMap();
				sql += "`" + key + "` = ? ";
				params.add(v);
				total += "`" + key + "` = :" + key + " ";
				totalParams.put(key, v);
				sqlMap.put("(" + sql + ")", params);
				sqlTotalMap.put("(" + total + ")", totalParams);
			}

		}
	}

	public Map<String, List<Object>> getSqlMap() {
		return sqlMap;
	}

	public void setSqlMap(Map<String, List<Object>> sqlMap) {
		this.sqlMap = sqlMap;
	}

	public Map<String, Object> getWebMap() {
		return webMap;
	}

	public Map<String, Map> getSqlTotalMap() {
		return sqlTotalMap;
	}

	public void setSqlTotalMap(Map<String, Map> sqlTotalMap) {
		this.sqlTotalMap = sqlTotalMap;
	}

	public String getOrderName() {
		return orderName;
	}

	public void setOrderName(String orderName) {
		this.orderName = orderName;
	}

	public String getAscDesc() {
		return ascDesc;
	}

	public void setAscDesc(String ascDesc) {
		this.ascDesc = ascDesc;
	}
}
